<template>
  <div>
    <h1>登陆</h1>
    <el-form ref="form" :model="form" label-width="80px">
      <el-form-item label="用户名">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="form.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.prevent="login">登陆</el-button>
        <el-button>取消</el-button>
      </el-form-item>
    </el-form>
  </div>

</template>

<script>
export default {
  name: "login",
  data() {
    return {
      form: {
        name: null,
        password: null
      }
    }
  },
  methods: {
    login() {
      let params = new URLSearchParams();
      params.append('username', this.form.name);
      params.append('password', this.form.password);

      this.$http.post("/login", params).then((resp) => {
        if (resp.data.code == 200) {
          console.log(resp.data.code);
          localStorage.setItem("account", JSON.stringify(resp.data));
          this.$router.push("/main");
        } else {
          this.$message({
            message: resp.data.message,
            type: 'error'
          });
        }
        console.log(resp);
      })


      // if (true){
      //   localStorage.setItem("account",JSON.stringify());
      //   this.$router.push("/main");
      // }
    }
  }

}


</script>

<style scoped>

</style>